package com.aplink.generic.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.aplink.generic.pojo.googlemaps.DirectionRequest;
import com.aplink.generic.pojo.googlemaps.DirectionsLeg;
import com.aplink.generic.pojo.googlemaps.DirectionsResult;
import com.aplink.generic.pojo.googlemaps.DirectionsRoutes;
import com.aplink.generic.pojo.googlemaps.DirectionsStep;
import com.aplink.generic.pojo.googlemaps.Distance;
import com.aplink.generic.pojo.googlemaps.Duration;
import com.aplink.generic.webservice.HttpResponseStatus;
import com.aplink.generic.webservice.HttpTask;
import com.aplink.generic.widgets.ApLinkGoogleMaps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetDirectionTask extends
        AsyncTask<DirectionRequest, Void, DirectionsResult> {
	private final Handler mHandler;

	public GetDirectionTask(Handler handler) {
		mHandler = handler;
	}

	@Override
	protected DirectionsResult doInBackground(DirectionRequest... params) {
		try {
			final HttpParams httpParameters = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParameters,
			        HttpTask.CONNECTION_TIME_OUT);
			HttpConnectionParams.setSoTimeout(httpParameters,
			        HttpTask.SO_TIME_OUT);
			final HttpClient httpClient = new DefaultHttpClient(httpParameters);
			HttpUriRequest request = new HttpPost(params[0].getRequestURL());
			HttpResponse response = httpClient.execute(request);
			final StatusLine statusLine = response.getStatusLine();
			final HttpResponseStatus statusCode = HttpResponseStatus
			        .get(statusLine.getStatusCode());
			if (statusCode.equals(HttpResponseStatus.SUCCESS)) {
				final HttpEntity entity = response.getEntity();
				final InputStream content = entity.getContent();
				final BufferedReader reader = new BufferedReader(
				        new InputStreamReader(content));
				String line;
				StringBuilder stringBuilder = new StringBuilder();
				while ((line = reader.readLine()) != null) {
					stringBuilder.append(line);
				}
				Gson gson = new GsonBuilder()
				        .registerTypeAdapter(DirectionsResult.class,
				                new DirectionsResult())
				        .registerTypeAdapter(DirectionsLeg.class,
				                new DirectionsLeg())
				        .registerTypeAdapter(DirectionsRoutes.class,
				                new DirectionsRoutes())
				        .registerTypeAdapter(DirectionsStep.class,
				                new DirectionsStep())
				        .registerTypeAdapter(Distance.class, new Distance())
				        .registerTypeAdapter(Duration.class, new Duration())
				        .create();
				return gson.fromJson(stringBuilder.toString(),
				        DirectionsResult.class);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(DirectionsResult result) {
		super.onPostExecute(result);
		Message.obtain(mHandler, ApLinkGoogleMaps.GET_DIRECTION, result)
		        .sendToTarget();
	}
}
